package cn.edu.buaa.neo.query.cxx.misuse.matchProblem;

import cn.edu.buaa.neo.codegraphql.core.graph.types.PointerType;
import cn.edu.buaa.neo.codegraphql.core.graph.values.statements.expressions.Literal;
import cn.edu.buaa.neo.codegraphql.core.graph.values.statements.expressions.Reference;
import cn.edu.buaa.neo.codegraphql.core.graph.values.statements.expressions.UnaryOperator;
import cn.edu.buaa.neo.codegraphql.dsl.fluent.query.QueryDescriptor;
import cn.edu.buaa.neo.codegraphql.dsl.lib.engine.QueryEngine;
import cn.edu.buaa.neo.codegraphql.language.cxx.CxxQuery;
import cn.edu.buaa.neo.codegraphql.language.shared.predicate.ContainsBinaryOperator;

import java.io.PrintStream;

public class CalculateArrayOffset extends CxxQuery {
    public static void main(String[] args) {
        QueryEngine.getInstance()
                .execute("CalculateArrayOffset", new CalculateArrayOffset())
                .close();
    }

    @Override
    public void run(String style, PrintStream output) {
        QueryDescriptor.open()
                .from("source", new ContainsBinaryOperator(
                        expression -> expression instanceof Reference reference
                                && reference.getType() instanceof PointerType,
                        value -> value.toStream().anyMatch(
                                v -> v instanceof UnaryOperator unary &&
                                        "sizeof".equals(unary.getOperator()) &&
                                        unary.getOperand() instanceof Literal),
                        null))
                .select("source")
                .display();
    }
}
